package com.meritdata.cloud.data.manager.model.controller;

import com.meritdata.cloud.data.base.ResultData;
import com.meritdata.cloud.data.manager.model.service.IDataModelService;
import com.meritdata.cloud.data.manager.model.service.impl.DataModelService;
import com.meritdata.cloud.data.standard.directory.entity.TdDataDomain;
import com.meritdata.cloud.data.standard.directory.service.IDataLayerService;
import com.meritdata.cloud.data.standard.directory.service.impl.DataDomainService;
import com.meritdata.cloud.data.standard.directory.service.impl.DataLayerService;
import com.meritdata.cloud.resultmodel.ResultBody;
import com.meritdata.cloud.resultmodel.ResultStatus;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/manager/model")
public class DataModelController {
    @Autowired
    private IDataLayerService dataLayerService;
    @Autowired
    private IDataModelService dataModelService;

    /**
     * 查找所有的物理模型
     * @param request request
     * @param pageNum pageNum
     * @param domainId 域ID
     * @param pageSize pageSize
     * @param dataSourceId  数据源ID
     * @param name 搜索名称
     * @return ResultBody
     */
    @PostMapping("/findByPage")
    public ResultBody findByPage(HttpServletRequest request, @RequestParam int pageNum,
                                 @RequestParam int pageSize,
                                 @RequestParam Long domainId ,
                                 @RequestParam String name,
                                 @RequestParam Long dataSourceId
    ){
        Map<String,Object> map=new HashMap<>();
        map.put("pageNum",pageNum);
        map.put("pageSize",pageSize);
        map.put("name",name);
        map.put("domainId",domainId);
        map.put("dataSourceId",dataSourceId);
        try{
            return ResultBody.success(dataModelService.findByPage(map));
        }catch (RuntimeException e){
            return ResultBody.failure(ResultStatus.DATA_OPERATION_ERROR,e);
        }

    }


    /**
     * 根据ID查询的物理模型
     * @ResultBody ResultBody
     */
    @PostMapping("/findById")
    public ResultBody findById(@RequestParam Long id){
        try{
            return ResultBody.success(dataModelService.findById(id));
        }catch (RuntimeException e){

            return ResultBody.failure(ResultStatus.DATA_OPERATION_ERROR,e);
        }

    }

    @PostMapping("/findAllLayer")
    public ResultBody getTdDataLayerList(){
        try{
            return ResultBody.success(dataLayerService.list());
        }catch (RuntimeException e){
            return ResultBody.failure(ResultStatus.DATA_OPERATION_ERROR,e);
        }
    }
    //
    @PostMapping("/domainTree")
     public ResultBody domainTree(@ApiParam(value = "数据层id",required = true) @RequestParam Long id){
        try{
            return ResultBody.success(dataModelService.domainTree(id));
        }catch (RuntimeException e){
            return ResultBody.failure(ResultStatus.DATA_OPERATION_ERROR,e);
        }
     }
}
